const app = getApp();
import { queryByUserId } from '../../api/wallet.js';
import { queryByUserIdCoupon } from '../../api/userCoupon.js'; 
Page({
  data: {
    userPointInfo: {
      id: null,
      userId: null,
      points: 0,
      couponId: null,
      groupBuyId: null
    },
    couponList: [] 
  },

  onLoad() {
    this.loadPointData();
    this.loadCouponData(); 
  },

  navToMyDetail() {
    wx.switchTab({ url: '/pages/myDetail/myDetail' });
  },

  navToCouponExchange() {
    const currentPoints = this.data.userPointInfo.points || 0;
    wx.navigateTo({ url: `/pages/coupon/coupon?points=${currentPoints}` });
  },
  async loadPointData() {
      const res = await queryByUserId();
      console.log(res);
      if (res.code === 200 && res.data) {
        this.setData({ userPointInfo: res.data });
      } else {
        wx.showToast({ title: '积分加载失败', icon: 'none' });
      }
  },

  async loadCouponData() {
      const res = await queryByUserIdCoupon();
      console.log(res);
      if (res.code === 200 && Array.isArray(res.data)) {
        this.setData({ couponList: res.data });
      } else {
        wx.showToast({ title: '优惠券加载失败', icon: 'none' });
      }
  }
});